<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

require_once JPATH_COMPONENT_ADMINISTRATOR . '/helpers/autogenerator.php';

class AutogeneratorModelComponent extends JModelAdmin
{

    protected $text_prefix = 'COM_AUTOGENERATOR';
    
    public function getForm($aData = array(), $mLoadData = true)
    {
        $oForm = $this->loadForm('com_autogenerator.component', 'component', array('control' => 'jform', 'load_data' => $mLoadData));

        if (empty($oForm))
        {
            return false;
        }

        return $oForm;
    }

    public function getTable($sType = 'Component', $sPrefix = 'AutogeneratorTable', $aConfig = array())
    {
        return JTable::getInstance($sType, $sPrefix, $aConfig);
    }

    protected function loadFormData()
    {
        $aData = JFactory::getApplication()->getUserState('com_autogenerator.edit.component.data', array());

        if (empty($aData))
        {
            $aData = $this->getItem();
        }

        return $aData;
    }

    protected function getReorderConditions($oTable)
    {
        $aCondition = array();

        return $aCondition;
    }

    protected function prepareTable($oTable)
    {
        if (empty($oTable->id))
        {
            if (empty($oTable->ordering))
            {
                $oDatabase = JFactory::getDbo();
                
                $oDatabase->setQuery('SELECT MAX(ordering) FROM #__auto_component');
                
                $iMax = $oDatabase->loadResult();

                $oTable->ordering = $iMax + 1;
            }
        }
    }

    /**
     * For ajax. Change the status of the field.
     * @param int $iFieldId
     * @return int|string 
     */
    public function changeStatus($iFieldId) {
        $iKey = (int) $iFieldId;
        if ($iKey < 1) {
            return JText::_('COM_AUTOGENERATOR_COMPONENT_AJAX_ERROR_MESSAGE_COMPONENT_NOT_VALID');
        }

        $oTable = $this->getTable();

        // Allow an exception to be thrown.
        try {
            // Load the row if saving an existing record.
            if ($iKey > 0) {
                if (!$oTable->load($iKey)) {
                    return JText::_('COM_AUTOGENERATOR_COMPONENT_AJAX_ERROR_MESSAGE_COMPONENT_NOT_VALID');
                }
            }

            if (intval($oTable->id) == 0) {
                return JText::_('COM_AUTOGENERATOR_COMPONENT_AJAX_ERROR_MESSAGE_COMPONENT_NOT_VALID');
            }

            // Change the status.
            if ($oTable->status == 1)
                $oTable->status = 0;
            else
                $oTable->status = 1;

            // Prepare the row for saving
            $this->prepareTable($oTable);

            // Check the data.
            if (!$oTable->check()) {
                return $oTable->getError();
            }

            // Store the data.
            if (!$oTable->store()) {
                return $oTable->getError();
            }

            // Clean the cache.
            $this->cleanCache();
        } catch (Exception $e) {
            return $e->getMessage();
        }

        return $oTable->status;
    }
    
    public function delete(&$pks) {
        $oModelFields = JModelLegacy::getInstance('Fields', 'AutogeneratorModel');
        $oModelMvcs = JModelLegacy::getInstance('Mvcs', 'AutogeneratorModel');
        $oModelTables = JModelLegacy::getInstance('Tables', 'AutogeneratorModel');
        
        foreach ($pks as $iComponentId) {
            $oModelFields->deleleAllFieldsByComponentId($iComponentId);
            $oModelMvcs->deleleAllMVCsByComponentId($iComponentId);
            $oModelTables->deleleAllTablesByComponentId($iComponentId);
        }
        
        return parent::delete($pks);
    }
}
